<%
  if user_signed_in? and current_subject
    resources = subject_resources(current_subject, {:scope => :me, :limit => 0})
    excursions = subject_excursions(current_subject, {:scope => :me, :limit => 0}).reject{ |e| e.draft==true }
  else
    resources = []
    excursions = []
  end
%>

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
	<h1><%= I18n.t('workshop.activities.assignment.modal.title') %></h1>
</div>
<div class="modal-body">
  <div class="joining_tabs clearfix">
    <ul class="nav nav-tabs">
      <li class="active"><a class="url_choose_wa" data-toggle="tab"><%=t("workshop.activities.assignment.modal.url_tab")%></a></li>
      <li><a class="excursions_choose_wa" data-toggle="tab"><%=t("search.models.excursion")%></a></li>
      <li><a class="resources_choose_wa" data-toggle="tab"><%=t("search.models.resource")%></a></li>
    </ul>
    <div class="url_choose_wa" class="tab-pane">
      <div><p><%=t("workshop.activities.assignment.modal.url_explanation")%></p></div>
      <input class="url_choose_wa_input" type="text" placeholder="<%=Vish::Application.config.full_domain%>/..."/>
    </div>
    <div class="excursions_choose_wa" class="tab-pane">
      <% unless excursions.blank? %>
        <ul class="items">
          <% with_format('html') do %>
            <% excursions.reject{|r| r.nil? or r.object.nil?}.each do |r| %>
              <%= render :partial => '/entities/entity', :formats => [:html], :locals => { :entity => r, :prefix_id => "contribution" } %>
            <% end %>
          <% end %>
        </ul>
      <% else %>
        <% if !current_subject.nil? %>
          <div class="no_items"><%= I18n.t('workshop.activities.assignment.modal.notfound') %></div>
        <% end %>
      <% end %>
    </div>
    <div class="resources_choose_wa" class="tab-pane">
      <% unless resources.blank? %>
        <ul class="items">
          <% with_format('html') do %>
            <% resources.reject{|r| r.nil? or r.object.nil?}.each do |r| %>
              <%= render :partial => '/entities/entity', :formats => [:html], :locals => { :entity => r, :prefix_id => "contribution" } %>
            <% end %>
          <% end %>
        </ul>
      <% else %>
        <% if !current_subject.nil? %>
          <div class="no_items"><%= notfound %></div>
        <% end %>
      <% end %>
    </div> 
  </div>
</div>
<div class="modal-footer">
  <div class="footer-form clearfix">
    <a type="button" class="btn btn-primary button_for_contribution"><%= I18n.t('workshop.activities.assignment.modal.bt_upload') %></a>
  </div>
</div>

<script type="text/javascript">
  $(document).ready(function(){

    var formId = "<%=params[:formid]%>";
    var form = $("#" + formId);
    var formButton = $(form).find(".button_for_contribution");

    $(form).find("a.url_choose_wa").attr("href","#"+formId+"_url_choose_wa");
    $(form).find("div.url_choose_wa").attr("id",formId+"_url_choose_wa");
    $(form).find("a.excursions_choose_wa").attr("href","#"+formId+"_excursions_choose_wa");
    $(form).find("div.excursions_choose_wa").attr("id",formId+"_excursions_choose_wa");
    $(form).find("a.resources_choose_wa").attr("href","#"+formId+"_resources_choose_wa");
    $(form).find("div.resources_choose_wa").attr("id",formId+"_resources_choose_wa");

    $(form).find(".joining_tabs").tabs();
    $(formButton).attr("disabled", "disabled");

    $(form).find(".box-item a").each(function(e){
      $(this).attr('url',$(this).attr('href'));
      $(this).removeAttr('href');
    });

    $(form).find(".box-item").each(function(e){
      $(this).prepend('<div class="selecting-item" style="display:none;"><i class="fa fa-check"></i>' + '<%=t('competition.modal.selected')%>' + '</div>');
    });

    $(form).find(".box-item").on("click",function(){
      if($(formButton).attr("submitting")==="true"){
        return;
      }
      if(!$(this).hasClass("selected")){
        $(form).find(".box-item").removeClass("selected");
        $(this).addClass("selected");
        $(formButton).removeAttr("disabled");
      } else {
        $(this).removeClass("selected");
        $(formButton).attr("disabled", "disabled");
      }
    });

    $(form).find(".url_choose_wa_input").on("keyup",function(){
      var inputTabActive = $(form).find(".joining_tabs a.url_choose_wa").parent("li").hasClass("active");
      if(($(formButton).attr("submitting")==="true")||(inputTabActive===false)){
        return;
      }
      if($(this).val().trim()!=""){
        $(formButton).removeAttr("disabled");
      } else {
        $(formButton).attr("disabled", "disabled");
      }
    });

    $(form).find('a[data-toggle="tab"]').on('shown.bs.tab',function(e){
      if($(formButton).attr("submitting")==="true"){
        return;
      }
      $(form).find(".box-item").removeClass("selected");
      $(formButton).attr("disabled", "disabled");
    });

    $(formButton).on("click",function(){
      if($(this).attr("submitting")==="true"){
        return;
      }

      var inputTabActive = $(form).find(".joining_tabs a.url_choose_wa").parent("li").hasClass("active");
      if(inputTabActive){
        var selectedUrl = $(form).find(".url_choose_wa_input").val();
      } else {
        var selectedUrl = $(form).find('.selected a').attr("url");
      }

      if(typeof selectedUrl != "undefined"){
        var fullDomain = "<%= Vish::Application.config.full_domain %>";
        if(selectedUrl.indexOf(fullDomain)==-1){
          selectedUrl = fullDomain + selectedUrl;
        }

        $(this).attr("disabled", "disabled");
        $(this).attr("submitting","true");
        $(this).parents("div").addClass("cursor_waiting");
        $(form).find(".box-item").addClass("cursor_waiting");
        $(form).find(".box-item").find("a").addClass("cursor_waiting");
        
        var contribution_parent_key = $(form).attr("contribution-parent-key");
        var contribution_parent_value = $(form).attr("contribution-parent-value");
        var contribution = {
          type: "Resource"
        }
        contribution[contribution_parent_key] = contribution_parent_value;

        //Ajax request to submit selectedUrl
        $.ajax({
          url: "/contributions",
          method: "POST",
          dataType: "json",
          data: {
            contribution: contribution,
            url:  selectedUrl,
            authenticity_token: "<%= form_authenticity_token %>"
          },
          success: function(e){
            window.location.reload();
          },
          error: function(e){
            if((typeof e.responseJSON == "object")&&(e.responseJSON.errors instanceof Array)&&(e.responseJSON.errors.length > 0)){
              alert(e.responseJSON.errors[0]);
              $(".cursor_waiting").removeClass("cursor_waiting");
              $(formButton).removeAttr("submitting");
              $(formButton).removeAttr("disabled");
            }
          }
        });
        
      }
    });
  });
</script>